import axios from 'axios'
import type { AxiosInstance, InternalAxiosRequestConfig, AxiosResponse } from 'axios'

const service: AxiosInstance = axios.create({
  baseURL: 'http://8.140.225.174:5000',
  timeout: 5000
})

// 请求拦截器
service.interceptors.request.use(
  (config: InternalAxiosRequestConfig) => {
    const token = localStorage.getItem('token')
    if (token) {
      config.headers.set('Token', token)
    }
    return config
  },
  (error) => {
    return Promise.reject(error)
  }
)

// 响应拦截器
service.interceptors.response.use(
  (response: AxiosResponse) => {
    const res = response.data
    // 直接返回完整的响应数据，让业务层处理具体的状态码
    return res
  },
  (error) => {
    console.error('请求错误:', error)
    return Promise.reject(new Error(error.response?.data?.message || '网络错误'))
  }
)

export default service 